from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _

class User(AbstractUser):
    USER_TYPE_CHOICES = (
        ('consumer', '消费者'),
        ('chef', '厨师'),
        ('admin', '管理员'),
    )
    
    # 基础字段
    openid = models.CharField(max_length=64, unique=True, null=True, blank=True)  # 必须唯一
    unionid = models.CharField(max_length=64, null=True, blank=True)  # 可选
    nickname = models.CharField(max_length=50, blank=True)
    avatar = models.URLField(null=True, blank=True)  # 微信头像URL
    email = models.EmailField(_('email address'), blank=True, null=True, default=None)
    phone = models.CharField(max_length=20, null=True, blank=True)
    #avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)
    bio = models.TextField(max_length=500, blank=True)
    subscribe_status = models.BooleanField(default=False)  # 订阅状态
    points = models.IntegerField(default=0)
    user_type = models.CharField(max_length=10, choices=USER_TYPE_CHOICES, default='consumer')
    
    # 地址信息
    city = models.CharField(max_length=100, blank=True)
    district = models.CharField(max_length=100, blank=True)
    
    # 状态控制
    is_verified = models.BooleanField(default=False)
    last_active = models.DateTimeField(auto_now=True)

    @property
    def orders(self):
        """关联订单查询快捷方式"""
        from orders.models import Order
        return Order.objects.filter(user=self)

    # @property
    # def coupons(self):
    #     """关联优惠券查询快捷方式"""
    #     from coupons.models import UserCoupon
    #     return UserCoupon.objects.filter(user=self)

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return f"{self.username} ({self.get_user_type_display()})"